<?php namespace Admin\Controller;

use Common\Model\Goods;
use Common\Model\Cate;	
use Common\Model\Brand;
use Common\Model\Type;
use Common\Model\Type_attr;
	
	class GoodsController extends CommonController{
		private $db;
		private $CateDb;
		private $BrandDb;
		private $TypeDb;
		private $Type_attrDb;
				
	public function __init(){
		parent::__init();
		
		$this->db = new Goods;
		$this->CateDb = new Cate;
		$this->BrandDb = new Brand;
		$this->TypeDb = new Type;
		$this->Type_attrDb = new Type_attr;
		
		
	}
	
	public function index(){
		$total=$this->db->orderBy('gid','ASC')->count();
//		统计不在回收站内（is_recycle=0）	的文章的总数
			
//		实例化分页，生成分页模版	
		$page=Page::row(10)->make($total);
//		对所有文章进行分页处理，每页4条 row(4)	  
		View::with('page',$page);	
//		分配分页模版	
		
		
	$data=$this->db->orderBy('gid','ASC')->limit(Page::limit())->get();	
	
		View::with('data',$data);
		View::make();
	}
	
	
	public function add(){
		if(IS_POST){
//			$data=$_POST;
//			P($data);die;
			
//			var_dump($specData);die;
//			p($_POST);die;
			$result=$this->db->store();	
				if(!$result){
					View::error($this->db->getError());
				}
				
				View::success('添加成功',U('index'));
			}

		
		$cateData=$this->CateDb->get();		
//		var_dump($cateData);die;	
		$cateData=Data::tree($cateData,'cname');	
		View::with('cateData',$cateData);
		
		$brandData=$this->BrandDb->get();		
//		var_dump($brandData);die;		
		View::with('brandData',$brandData);
		
		View::make();
	}	
	
	
	public function getTypeAttr(){
//		echo 'ok';die;
		$tid=Q('post.tid',0,'intval');
//		var_dump($tid);die;
		$typeAttrData=$this->Type_attrDb->where('type_tid',$tid)->get();
		
//		p($typeAttrData);die;
//		在network中查看
		foreach($typeAttrData as $k=>$v){
			$typeAttrData[$k]['tavalue']=explode(',', $v['tavalue']);
		}
		
		View::ajax($typeAttrData);die;
	}
	
	
	public function upload()
{
	if(isset($_POST['pic'])){
		return $_POST['pic'];
	}
	
    $file = Upload::path('Upload/Content/Goods/' . date('y/m'))->make();
    if (empty($file)) {
        $this->ajax(Upload::getError());
    } else {
        /** $file内部就是以下这个数组
            $file = array(
                0 => array(
                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
                'image' => 1
            ),
        );**/
        //上传成功，把上传好的信息返给js
        $data = $file[0];
        echo json_encode($data);exit;
    }
}

	public function upload_2()
{
		
    $file = Upload::path('Upload/' . date('y/m'))->make();
//	var_dump($file);die;
    if (empty($file)) {
        $this->ajax(Upload::getError());
    } else {
        /** $file内部就是以下这个数组
            $file = array(
                0 => array(
                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
                'image' => 1
            ),
        );**/

        //上传成功，把上传好的信息返给js
        $data = $file[0];
        echo json_encode($data);exit;
    }
}
	
	
	
	public function del(){
		$gid=Q('get.gid',0,'intval');
//		获取bid
		$this->db->where('gid',$gid)->delete();
//		删除对应的商品
		
		$g_dModel= new \Common\Model\Goods_detail;		
		$g_dModel->where('goods_gid',$gid)->delete();
//		根据获取的商品id删除商品信息
		
		$g_aModel = new \Common\Model\Goods_attr;		
		$g_aModel->where('goods_gid',$gid)->delete();
//		根据获取的商品id删除商品属性
				
		View::success('删除成功',U('index'));
	}



	public function edit(){
		if(IS_POST){
//			var_dump($_POST);die;
//			
//			var_dump($specData);die;
//			p($_POST);die;
			$result=$this->db->edit();	
				if(!$result){
					View::error($this->db->getError());
				}
				
				View::success('编辑成功',U('index'));
			}
		
		
		
		$gid=Q('gid',0,'intval');
		
		
//		$oldData=$this->db->join('goods_attr','gid','=','goods_gid')->join('goods_detail','gid','=','goods_gid')->where('gid',$gid)->find();
		$oldData=Db::table('goods g')
		->join('goods_attr ga','g.gid','=','ga.goods_gid')
		->join('goods_detail gd','g.gid','=','gd.goods_gid')
		->join('type_attr ta','ga.type_attr_taid','=','ta.taid')		
		->where('g.gid',$gid)
		->get();		
//		var_dump($oldData);die;

//		}
		foreach($oldData as $k=>$v){
			$oldData[$k]['tavalue']=explode(',', $v['tavalue']);
			$oldData[$k]['photo']=explode(',', $v['photo']);
		}
//		p($oldData);die;
		View::with('oldData',$oldData);
		

				
//		p($oldattrData);die;
		
		$cateData=$this->CateDb->get();		
//		var_dump($cateData);die;	
		$cateData=Data::tree($cateData,'cname');	
		View::with('cateData',$cateData);
		
		$brandData=$this->BrandDb->get();		
//		var_dump($brandData);die;		
		View::with('brandData',$brandData);
		
		View::make();
	}








}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	?>